package com.github.xuemingyu.springboot.acticitycloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bidanet.bdcms.core.vo.Page;
import com.bidanet.hibernate.lambda.core.LambdaCriteria;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.github.xuemingyu.springboot.acticitycloud.dao.DepartmentDao;
import com.github.xuemingyu.springboot.acticitycloud.driver.dingtalk.DingTalkService;
import com.github.xuemingyu.springboot.acticitycloud.entity.Department;
import com.github.xuemingyu.springboot.acticitycloud.entity.excel.UserExcel;
import com.github.xuemingyu.springboot.acticitycloud.service.DepartmentService;
import com.github.xuemingyu.springboot.jkfaststart.common.LambdaQuery;
import com.taobao.api.ApiException;
import org.apache.commons.lang.StringUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private DingTalkService dingTalkService;

    @Override
    public void queryLike(Department department, Page<Department> page) {

    }

    @Override
    public void insert(Department department) {
        departmentDao.save(department);
    }

    @Override
    public List<String> getAuthority(String userId) throws ApiException {
        OapiUserGetResponse user = dingTalkService.getUserInfoByUserId(userId);
        String isLeaderInDepts = user.getIsLeaderInDepts();
        Map map = (Map) JSON.parse(isLeaderInDepts);
        List list = new ArrayList();
        List<String> userIdList = new ArrayList<>();
        if (Objects.nonNull(map)) {
            map.keySet().forEach(q -> {
                if (map.get(q).equals(true)) {
                    list.add(q);
                }
            });
            if (list.size() > 0) {
                LambdaCriteria<Department> lambdaCriteria = lambdaQuery.create(Department.class);
                list.forEach(s -> {
                    lambdaCriteria.or(w -> {
                        w.like(q -> {
                            q.setDepartmentList("%," + s + ",%");
                        });
                    });
                });
                List<Department> departmentList = lambdaCriteria.list();
                if (departmentList.size() > 0) {
                    departmentList.forEach(q -> {
                        userIdList.add(q.getUserId());
                    });
                }
            }
        }
        return userIdList;
    }

    @Override
    public Map<String, List<String>> getUserByDept(String dept) {
        List<String> userIdList = new ArrayList<>();
        List<String> mobileList = new ArrayList<>();
        LambdaCriteria<Department> lambdaCriteria = lambdaQuery.create(Department.class);
        if (StringUtils.isNotBlank(dept)) {
            lambdaCriteria.like(q -> {
                q.setDepartmentList("%," + dept + ",%");
            });
        }
        List<Department> list = lambdaCriteria.list();
        list.forEach(q -> {
            userIdList.add(q.getUserId());
            if (StringUtils.isNotBlank(q.getMobile())) {
                mobileList.add(q.getMobile());
            }
        });
        Map<String, List<String>> map = new HashMap();
        map.put("userId", userIdList);
        map.put("mobile", mobileList);
        return map;
    }

    @Override
    public void importUser(MultipartFile file) {
        ImportParams importParam = new ImportParams();
        List<UserExcel> userExcelList = null;
        if (file != null) {
            try {
                userExcelList = ExcelImportUtil.importExcel(file.getInputStream(), UserExcel.class, importParam);
                userExcelList.forEach(q ->{
                    String parentDepts = null;
                    try {
                        parentDepts = dingTalkService.getParentDepts(q.getUserId());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    OapiUserGetResponse user = null;
                    try {
                        user = dingTalkService.getUserInfoByUserId(q.getUserId());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    Department firstByUserId = departmentDao.findFirstByUserId(q.getUserId());
                    if (Objects.isNull(firstByUserId)) {
                        Department department = new Department();
                        department.setDepartmentList(parentDepts);
                        department.setUserId(q.getUserId());
                        department.setMobile(user.getMobile());
                        insert(department);
                    } else {
                        firstByUserId.setMobile(user.getMobile());
                        firstByUserId.setDepartmentList(parentDepts);
                        departmentDao.save(firstByUserId);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    protected JpaRepository<Department, Long> getDao() {
        return departmentDao;
    }
}
